package com.lz.autotest.project.system.controller;


import com.lz.autotest.common.aspectj.lang.annotation.Log;
import com.lz.autotest.common.aspectj.lang.enums.BusinessType;
import com.lz.autotest.project.system.domain.SysUser;
import com.lz.autotest.common.utils.SecurityUtils;
import com.lz.autotest.common.utils.StringUtils;
import com.lz.autotest.common.web.controller.BaseController;
import com.lz.autotest.common.web.domain.AjaxResult;
import com.lz.autotest.common.web.page.TableDataInfo;
import com.lz.autotest.project.system.domain.perm.*;
import com.lz.autotest.project.system.service.ISysUserPermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 用户权限操作处理
 *
 * @author lz
 */
@RestController
@RequestMapping("/system/userPerm")
public class SysUserPermController extends BaseController {


    @Autowired
    private ISysUserPermService userPermService;



    /**
     * 获取用户权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserPerm userPerm)
    {
        startPage();
        List<SysUserPerm> list = userPermService.selectUserPermList(userPerm);
        return getDataTable(list);
    }


    /**
     * 获取用户权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:list')")
    @RequestMapping("/userAccDetailPerm.action")
    public TableDataInfo userAccDetailPerm(@RequestBody AccUser accUser)
    {
        List<AccUser> list=new ArrayList<>();
        if(("1").equals(accUser.getAccFlag())){
            //全部时查询部门
            list= userPermService.selectDeptAccPermList(accUser);
        }else{
            list= userPermService.selectUserAccPermList(accUser);
        }
        return getDataTable(list);
    }
    /**
     * 获取用户权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:list')")
    @RequestMapping("/userGroupDetailPerm.action")
    public TableDataInfo userGroupDetailPerm(@RequestBody GroupUser groupUser)
    {
        List<GroupUser> list =new ArrayList<>();
        if(("1").equals(groupUser.getGroupFlag())){
            list = userPermService.selectDeptGroupPermList(groupUser);
        }else{
            list = userPermService.selectUserGroupPermList(groupUser);
        }
        return getDataTable(list);
    }
    /**
     * 获取用户权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:list')")
    @RequestMapping("/userCfgDetailPerm.action")
    public TableDataInfo userCfgDetailPerm(@RequestBody CfgUser cfgUser)
    {
        List<CfgUser> list =new ArrayList<>();
        if(("1").equals(cfgUser.getCfgFlag())){
            list=userPermService.selectDeptCfgPermList(cfgUser);
        }else{
            list=userPermService.selectUserCfgPermList(cfgUser);
        }
        return getDataTable(list);
    }

    /**
     * 获取用户权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:list')")
    @RequestMapping("/userIrcDetailPerm.action")
    public TableDataInfo userIrcDetailPerm(@RequestBody IrcUser ircUser)
    {
        List<IrcUser> list =new ArrayList<>();
        if(("1").equals(ircUser.getIrcFlag())){
            list=userPermService.selectDeptIrcPermList(ircUser);
        }else{
            list = userPermService.selectUserIrcPermList(ircUser);
        }
        return getDataTable(list);
    }

    /**
     * 获取用户利率权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:list')")
    @RequestMapping("/userDivDetailPerm.action")
    public TableDataInfo userDivDetailPerm(@RequestBody DivUser divUser)
    {
        List<DivUser> list =new ArrayList<>();
        if(("1").equals(divUser.getDivFlag())){
            list=userPermService.selectDeptDivPermList(divUser);
        }else{
            list=userPermService.selectUserDivPermList(divUser);
        }

        return getDataTable(list);
    }
    /**
     * 获取用户权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:list')")
    @RequestMapping("/userVlsDetailPerm.action")
    public TableDataInfo userIrcDetailPerm(@RequestBody VlsUser vlsUser)
    {
        List<VlsUser> list =new ArrayList<>();
        if(("1").equals(vlsUser.getVlsFlag())){
            list=userPermService.selectDeptVolPermList(vlsUser);
        }else{
            list=userPermService.selectUserVolPermList(vlsUser);
        }

        return getDataTable(list);
    }
    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:query')")
    @GetMapping(value = { "/","/{userId}" })
    public AjaxResult getInfo(@PathVariable (value = "userId",required = false) Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(userId))
        {
            SysUserPerm userPerm = userPermService.selectUserPermById(userId);
            ajax.put(AjaxResult.DATA_TAG, userPerm);
        }
        return ajax;
    }

    /**
     * 新增用户权限
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:add')")
    @Log(title = "用户权限管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysUserPerm userPerm)
    {

        return toAjax(userPermService.insertUserPerm(userPerm));
    }

    /**
     * 修改用户权限
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:edit')")
    @Log(title = "用户权限管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysUserPerm userPerm)
    {
        return toAjax(userPermService.updateUserPerm(userPerm));
    }

    /**
     * 清空用户权限
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:remove')")
    @Log(title = "用户权限管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        userPermService.clearUserPermDetail(userIds);
        return toAjax(userPermService.deleteUserPermByIdS(userIds));
    }
    /**
     * 获取用户账户权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:edit')")
    @GetMapping("/edtUserAccPerm.action")
    public AjaxResult edtUserAccPerm( AccUser accUser)
    {
        return toAjax(userPermService.edtUserAccPerm(accUser));
    }


    /**
     * 获取用户组合权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:edit')")
    @GetMapping("/edtUserGroupPerm.action")
    public AjaxResult edtUserGroupPerm( GroupUser groupUser)
    {
        return toAjax(userPermService.edtUserGroupPerm(groupUser));
    }


    /**
     * 获取用户配置权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:edit')")
    @GetMapping("/edtUserCfgPerm.action")
    public AjaxResult edtUserCfgPerm( CfgUser cfgUser)
    {
        return toAjax(userPermService.edtUserCfgPerm(cfgUser));
    }

    /**
     * 修改用户利率权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:edit')")
    @GetMapping("/edtUserIrcPerm.action")
    public AjaxResult edtUserIrcPerm( IrcUser ircUser)
    {
        return toAjax(userPermService.edtUserIrcPerm(ircUser));
    }

    /**
     * 修改用户红利权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:edit')")
    @GetMapping("/edtUserDivPerm.action")
    public AjaxResult edtUserDivPerm( DivUser divUser)
    {
        return toAjax(userPermService.edtUserDivPerm(divUser));
    }
    /**
     * 修改用户波动率权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:userperm:edit')")
    @GetMapping("/edtUserVlsPerm.action")
    public AjaxResult edtUserVlsPerm( VlsUser vlsUser)
    {
        return toAjax(userPermService.edtUserVlsPerm(vlsUser));
    }

    @PostMapping(value="/filterAuth.action")
    public List<Map> getFilterAuth(@RequestBody List<Map> list, @RequestHeader("authType") String authType)
    {
        List<Map> resultList= userPermService.filterAuth(list,authType);
        return resultList;
    }
    @GetMapping(value="/getPermFlag.action")
    public String getPermFlag( @RequestParam ("authType") String authType)
    {
        return  userPermService.getPermFlag(authType);
    }

    @GetMapping("/listCfgAllocateUser")
    public TableDataInfo getCfgAllocateUser(@RequestParam Map<String,Object> params)
    {
        startPage();
        params.put("userId", SecurityUtils.getUserId());
        List<SysUser> list = userPermService.selectCfgAllocateUserList(params);
        return getDataTable(list);
    }

}
